package handlers

import (
	"errors"

	"github.com/yjp211/web"

	"mutu/misc/typedef"
	"mutu/models"
	"mutu/service"
)

type DownloadView struct{}

var (
	DownloadHandler = DownloadView{}
)

func (p DownloadView) GetUrl(ctx *web.Context) string {
	matterid, ok := ctx.Params["matterid"]
	if !ok {
		return JsonpReturn(ctx, nil, errors.New("参数错误"))
	}

	ses := ctx.Dict["session"].(*models.Session)

	down := &models.DownloadRecord{
		UserId:   ses.UserId,
		MatterId: matterid,
	}

	link, err := service.DownloadService.GetDownloadUrl(down)
	if err != nil {
		return JsonpReturn(ctx, nil, err)
	}
	data := typedef.Dict{
		"Link": link,
	}
	return JsonpReturn(ctx, data, nil)
}

func (p DownloadView) GetRecords(ctx *web.Context) string {

	ses := ctx.Dict["session"].(*models.Session)

	arr, err := service.DownloadService.GetConvertRecords(ses.UserId)
	if err != nil {
		return JsonpReturn(ctx, nil, err)
	}
	service.MatterService.FillMattersInfo(arr, true, false, false, false)
	data := typedef.Dict{
		"matters": arr,
	}
	return JsonpReturn(ctx, data, nil)
}
